package 算法.leetcode.tree;

/**
 * @author lchenglong
 * @date 2020/11/9
 */
public class SameTree {
    public static boolean isSameTree(TreeNode p, TreeNode q) {
        if (p == null && q!=null || q == null  && p!=null) {
            return false;
        }
        if (p == q) {
            return true;
        }
        if ( p.val == q.val) {
            if (isSameTree(p.left,q.left) && isSameTree(p.right,q.right)) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    public static void main(String[] args) {
        TreeNode p = new TreeNode(1);
        TreeNode pl = null;
        TreeNode pr = new TreeNode(3);
        p.left = pl;
        p.right = pr;

        TreeNode q = new TreeNode(1);
        TreeNode ql = new TreeNode(2);
        q.left = ql;
        System.out.println(isSameTree(p,q));

    }
    static class TreeNode {

        int val;

        TreeNode left;

        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
